
Day25_符合p圖輪廓的變形圖片_merge_two_images_transform.ipynb
我們該來運用之前學過的所有東西了!
綜合運用篇就是來一次運用前面的所學!
今天的功能與昨天的內容非常像,而我們今天來把圖片p到想要的地方:
#Read the destination image
ori_img = cv2.imread("./testdata/tv.jpg")
print("origin image: ")
show_img(ori_img)
print("Click on four corners of bllboard and the press ENTER")
points = get_points(ori_img) 
這部分就單純的讀取圖片,
我們使用我們定義的 get_points 來幫助我們找到 輪廓的四個座標點。
def mouse_handler(event, x, y, flags, data):
    if event == cv2.EVENT_LBUTTONDOWN:
        # 標記點位置
        cv2.circle(data['img'], (x,y), 3, (0,0,255), -1) 
        # 改變顯示 window 的內容
        cv2.imshow("Image", data['img'])
        
        # 顯示 (x,y) 並儲存到 list中
        print("get points: (x, y) = ({}, {})".format(x, y))
        data['points'].append((x,y))
def get_points(img):
    # 建立 data dict, img:存放圖片, points:存放點
    data = {}
    data['img'] = img.copy()
    data['points'] = []
    
    # 建立一個 window
    cv2.namedWindow("Image", 0)
    
    # 改變 window 成為適當圖片大小
    h, w, dim = img.shape
    print("img height, width: ({}, {})".format(h, w))
    cv2.resizeWindow("Image", w, h)
        
    # 顯示圖片在 window 中
    cv2.imshow('Image',img)
    
    # 利用滑鼠回傳值,資料皆保存於 data dict中
    cv2.setMouseCallback("Image", mouse_handler, data)
    
    # 等待關閉視窗,藉由 OpenCV 內建函數釋放資源
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # 回傳點 list
    return data['points']
這部分都與前天的成品相同,可參考昨天的描述。
這是我們取的四個點:
(注意:請從左上角,依照順時針順序,在圖片的四個角落點四個點)

# 垂直堆疊點
points_2D = np.vstack(points).astype(float)
print("\npoints list:")
print(points_2D)
我們將回傳的點座標堆疊成一個 2D list。
p_img = cv2.imread("./testdata/cat.jpg")
h, w, dim = p_img.shape
pst_src = np.array(
    [
        [0,0],
        [w-1,0],
        [w-1,h-1],
        [0,h-1]
     ],dtype=float
)
h, status = cv2.findHomography(pst_src, points_2D)
# print(h)
# 透視投影 (建立變形後的圖)
print("image after reshape: ")
im_temp = cv2.warpPerspective(p_img, h, (ori_img.shape[1], ori_img.shape[0]))
show_img(im_temp)
透視後的圖片大概是長這樣~~~

# 填充多邊形 - 黑色 (該區域全為0),等同於覆蓋上 mask
print("add mask: ")
cv2.fillConvexPoly(ori_img, points_2D.astype(int), 0, 16)
show_img(ori_img)

# add wraped source image to destination image
print("result image: ")
ori_img = cv2.add(ori_img, im_temp)
show_img(ori_img)

https://zhuanlan.zhihu.com/p/143035374
https://kknews.cc/code/3oqxejy.html
https://blog.csdn.net/fanjiule/article/details/81606596
https://blog.csdn.net/yefcion/article/details/79435591
https://blog.csdn.net/fengyeer20120/article/details/87798638